package com.wxxymaker.controller;

import com.wxxymaker.interceptor.annocation.AuthRequire;
import com.wxxymaker.model.entity.Stage;
import com.wxxymaker.service.*;
import com.wxxymaker.utils.QRCodeUtil;
import com.wxxymaker.utils.web.ResponseWarp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Soft
 * @Date: 2018/5/6 1:00
 * @Desc: 打分控制
 */
@RestController
@RequestMapping("/mark")
public class MarkHandler {
    private static Logger log = LoggerFactory.getLogger(MarkHandler.class);

    @Autowired
    private MarkService markService;

    @Autowired
    private StageService stageService;

    @Autowired
    private StageCountService stageCountService;

    @Autowired
    private ShopService shopService;

    /*
     * 暂停当前打分
     *
    @PostMapping("/pause")
    public ResponseWarp pause() {
        QRCodeUtil.clear();
        if (markService.setCan(false)) {
            return ResponseWarp.ok("暂停成功！");
        }
        return ResponseWarp.error("暂停失败！");
    }
    */

    /**
     * 更新二维码 指定打分项
     * @param request  请求
     * @param stageId  阶段id
     * @param targets  要被打分的项
     * @return 响应
     */
    @PostMapping("/flushQr")
    public ResponseWarp flushQr(HttpServletRequest request, Integer stageId,
                                @RequestParam(name = "targets[]", required = false) Integer[] targets) {
        if (markService.isCan()) {
            QRCodeUtil.QRCode code = QRCodeUtil.flush(request, stageId, targets);
            return ResponseWarp.ok(code);
        }
        return ResponseWarp.error("更新失败！");
    }

    /**
     * 重新开始当前的打分
     * @param request  HttpServletRequest
     * @param stage 必须包涵当前的阶段 id
     * @see AdminHandler stage在mark方法中设置
     */
     @PostMapping("/restart")
     public ResponseWarp restart(HttpServletRequest request, Stage stage) {
        try {
            QRCodeUtil.QRCode code = null;
            if (!markService.isCan()) {
                if(markService.Switch(true, stage)) {
                    code = QRCodeUtil.flush(request, stage.getId(), null);
                    stageCountService.deleteByStage(stage); // 清除阶段统计记录
                    return ResponseWarp.ok(code);
                }
            }
            return ResponseWarp.error("请先结束当前阶段打分！");
        } catch (Exception e) {
            log.error("打分重启失败@{}", e.getMessage());
        }
        return ResponseWarp.error("重新开始失败！");
     }

    /**
     * 打分控制开关切换
     * @param can 1 = true  0 = false
     * @param stage 打分阶段。打开打分时 设置打分阶段 关闭打分时 清空打分阶段。
     *              包括阶段id、阶段名、阶段类型（关闭时需要）
     */
    @PostMapping("/switch/{can}")
    public ResponseWarp markSwitch(@PathVariable Integer can, Stage stage, HttpServletRequest request) {
        QRCodeUtil.QRCode code = null;
        if (markService.Switch(can == 1, stage)) {
            if (can < 1) { // 关闭该阶段打分
                stage.extend(stageService.getOne(stage.getId()));
                // 添加打分阶段计算统计, 异步执行
                stageCountService.countScoreAndSave(stage);
                stageService.updateEndTime(stage.getId()); // 更新结束时间
                QRCodeUtil.clear();
            } else {
                code = QRCodeUtil.flush(request, stage.getId(), null);
            }
            return ResponseWarp.ok(code, "切换成功！");
        }
        return ResponseWarp.error("切换失败！");
    }

    /**
     * 判断当前是否可以打分
     */
    @AuthRequire
    @PostMapping("/can")
    public Boolean isCan() {
        return markService.isCan();
    }
}